package presentation;

import java.sql.Time;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.JTextPane;

import control.CControl;
import entity.ENode;
import entity.EPatternRegister;
import entity.EStringRegister;

public class PNodeErrorPanel extends JPanel implements Observer{
	
	private JTextPane p;
	private String[] nodes;
	private HashMap<String, Long> err;
	
	public PNodeErrorPanel(CControl cControl) {
		p = new JTextPane();
		this.add(p);
		
		err = new HashMap<String, Long>();
		
		nodes = cControl.getNodeList();
		
		for(int i =0 ; i<nodes.length;i++)
		{
			if(nodes[i].equalsIgnoreCase("MN"))
			{
				
			}
			else
			{
				cControl.addRegisterObserver(nodes[i], nodes[i] + "_ErrorReg", this);
				err.put(nodes[i] + "_ErrorReg",(long)0);
			}
		}
		
	}

	@Override
	public void update(Observable o, Object arg) {
		if(o instanceof EPatternRegister)
		{
		EPatternRegister e = (EPatternRegister) o;
		
		// read current err val
		long old_err = err.get(e.getName());
		long new_err = Long.parseLong((String)arg, 2);
		// get difference 
		long diff = new_err ^ old_err;
		long changed_set = diff & new_err;
		err.put(e.getName(), new_err);
		if(changed_set != 0)
		{
			p.setText(p.getText() + String.format("Time: %d\n", System.currentTimeMillis()));
			p.setText((p.getText() + e.getName() + ": " +  e.getTextValue(changed_set))+ "\n");
		}
		}
	}

}
